package hsp.admin.app.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hsp.admin.app.base.mapper.RoleAppRelationMapper;
import hsp.admin.app.base.po.RoleAppRelationPO;
import hsp.admin.app.base.service.IRoleAppRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class RoleAppRelationServiceImpl extends ServiceImpl<RoleAppRelationMapper, RoleAppRelationPO> implements IRoleAppRelationService {

    @Override
    public void saveBatchByRoleIdAndAppIds(long roleId, int[] appIds) {
        LambdaQueryWrapper<RoleAppRelationPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleAppRelationPO::getRoleId, roleId);
        remove(wrapper);

        if(appIds == null) return;
        List<RoleAppRelationPO> list = new ArrayList<>();
        Arrays.stream(appIds).forEach(appId -> {
            RoleAppRelationPO po = new RoleAppRelationPO();
            po.setRoleId(roleId);
            po.setAppId(appId);
            list.add(po);
        });
        saveBatch(list);
    }

}
